//
//  DatePickerView.h
//  YouOu
//
//  Created by weihong xuan on 2016/3/7.
//  Copyright © 2016年 weihong xuan. All rights reserved.
//


#define hScale ([UIScreen mainScreen].bounds.size.height) / 667
//获取屏幕 宽度、高度
#define SCREEN_WIDTH                                  ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT                                 ([UIScreen mainScreen].bounds.size.height)
#define ScreenWithRateFor6                            SCREEN_WIDTH/375.0

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#define Color_Black_Insurance                UIColorFromRGB(0x333333)
#define Color_AddAndReduce_BorderColor       UIColorFromRGB(0xcccccc)
#define Color_me_PickerView                  UIColorFromRGB(0xf4f5f9)
#define Color_me_PickerViewTitle             UIColorFromRGB(0x333333)
#define Color_me_PickerViewFinish            UIColorFromRGB(0xe9546b)

#import "DatePickerView.h"
#import "UIKitFactory.h"
#import "DateTimePickerView.h"

@interface DateTimePickerView ()

@property (nonatomic, strong) UIView            *dimView;
@property (nonatomic, strong) UIDatePicker      *datePicker;
@property (nonatomic, strong) UIButton          *btnFinish;

@end

@implementation DateTimePickerView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView {
    _dimView = [UIKitFactory createViewWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 260 * hScale) backgroundColor:[UIColor whiteColor] superView:self];
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideAnimation)];
    [self addGestureRecognizer:gesture];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    UIView *titleView = [UIKitFactory createViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44) backgroundColor:Color_me_PickerView superView:_dimView];
    [UIKitFactory createLabelWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44) text:@"年龄" font:[UIFont systemFontOfSize:15] textColor:Color_me_PickerViewTitle textAlignment:NSTextAlignmentCenter SuperView:titleView];
    _btnFinish = [UIButton buttonWithType:UIButtonTypeCustom];
    [titleView addSubview:_btnFinish];
    _btnFinish.titleLabel.font = [UIFont systemFontOfSize:15];
    [_btnFinish setTitle:@"完成" forState:UIControlStateNormal];
    [_btnFinish addTarget:self action:@selector(finishBtnEvent) forControlEvents:UIControlEventTouchUpInside];
    [_btnFinish setTitleColor:Color_me_PickerViewFinish forState:UIControlStateNormal];
    _datePicker = [[UIDatePicker alloc] init];
    [_dimView addSubview:_datePicker];
    _datePicker.datePickerMode = UIDatePickerModeDate;
    _datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh"];
}

- (void)finishBtnEvent {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    if (_datetimeBlock) {
        _datetimeBlock([dateFormatter stringFromDate:_datePicker.date]);
    }
    [self hideAnimation];
}

- (void)hideAnimation {
    
    [UIView animateWithDuration:0.5 animations:^{
        
        CGRect frame = _dimView.frame;
        frame.origin.y = SCREEN_HEIGHT;
        _dimView.frame = frame;
        self.alpha = 0.3;
        
    } completion:^(BOOL finished) {
        
        self.alpha = 0;
        [_dimView removeFromSuperview];
        [self removeFromSuperview];
        
    }];
    
}

- (void)showAnimation {
    
    [UIView animateWithDuration:0.5 animations:^{
        
        CGRect frame = _dimView.frame;
        frame.origin.y = SCREEN_HEIGHT-260*hScale;
        _dimView.frame = frame;
        
    }];
    
}

@end
